from enum import Enum
import numpy as np

class BaseEnum(Enum):

    def order(self)->int:
        order = -1
        for name in self.__class__._member_names_:
            order += 1
            if self.name == name:
                return order
        raise RuntimeError("should't not run here.")

    @classmethod
    def parse_order(cls,order:int):
        name =  cls._member_names_[order]
        return cls._member_map_[name]


class SimpleQueue():
    """
    队列。如果满的话，遵循先进先出FIFO原则，剔除最旧的数据。最新的数据总是保存在values里面。
    """

    def __init__(self, full_size, is_number = False):
        self.full_size = full_size
        self._values: np.ndarray =  np.zeros(full_size) if is_number else np.full(full_size,any)
        for i in range(0,full_size):
            self._values[i] = None
        self.total_count = 0

    @property
    def values(self) -> np.ndarray:
        if self.is_full():
            return self._values
        else:
            _size = self.size()
            if _size > 0:
                return self._values[-_size:]
            return []

    def clear(self):
        self.total_count = 0

    def add(self, value):
        """
        Update new bar data into array manager.
        """
        self._values[:-1] = self._values[1:]
        self._values[-1] = value
        self.total_count +=1

    def get(self,index):
        return self._values[index]

    def first(self):
        _size = self.size()
        if _size == 0:
            return None
        return self._values[-_size]

    def size(self):
        if self.is_full():
            return self.full_size
        else:
            return self.total_count

    def last(self):
        if self.size() == 0:
            return None
        return self._values[-1]


    def is_full(self):
        return self.total_count >= self.full_size
